package com.faradel.pharmapp.managedbean;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.faradel.pharmapp.entities.Patient;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.faradel.pharmapp.service.PatientServiceInterface;
import com.faradel.pharmapp.service.ProviderServiceInterface;

@ManagedBean
@ViewScoped
public class ProcessPatientBean implements Serializable
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Resource
    private Patient patient;

    private String patientId;

    private String action;

    private String providerName;

    private String dateOfBirth;

    private String age;

    private Date maxDate;

    /**
     * Initialize
     */
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        patient = (Patient) context.getBean("patient");

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        action = params.get("action");
        patientId = params.get("patientId");

        maxDate = TimeClass.getCurrentDate();

        if (!action.equals("create"))
        {
            patient = getPatientServiceInterface().getPatientById(Integer.parseInt(patientId));
            providerName = getProviderServiceInterface().getProviderById(patient.getProviderId()).getProvider_name();
            age = String.valueOf(patient.getAge());
        }

    }

    private PatientServiceInterface getPatientServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (PatientServiceInterface) context.getBean("patientServiceImpl");
    }

    private ProviderServiceInterface getProviderServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (ProviderServiceInterface) context.getBean("providerServiceImpl");
    }

    public void createPatient()
    {
        patient.setAge(Integer.parseInt(age));
        getPatientServiceInterface().addPatient(patient);
    }

    public void deletePatient()
    {
        getPatientServiceInterface().deletePatient(patient);
    }

    public void updatePatient()
    {
        patient.setAge(Integer.parseInt(age));
        getPatientServiceInterface().updatePatient(patient);
    }

    public void reset()
    {

    }

    // private void convertDateToString()
    // {
    // DateFormat df = new SimpleDateFormat("y-MM-d");
    // dateOfBirth = df.format(patient.getDob());
    // }

    // private Date convertStringToDate(String s)
    // {
    // Date d = TimeClass.getCurrentDate();
    // try
    // {
    // d = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(dateOfBirth);
    // }
    // catch (ParseException e)
    // {
    // e.printStackTrace();
    // }
    //
    // return d;
    // }

    public Patient getPatient()
    {
        return patient;
    }

    public void setPatient(Patient patient)
    {
        this.patient = patient;
    }

    public String getPatientId()
    {
        return patientId;
    }

    public void setPatientId(String patientId)
    {
        this.patientId = patientId;
    }

    public String getAction()
    {
        return action;
    }

    public void setAction(String action)
    {
        this.action = action;
    }

    public String getProviderName()
    {
        return providerName;
    }

    public void setProviderName(String providerName)
    {
        this.providerName = providerName;
    }

    public String getDateOfBirth()
    {
        return dateOfBirth;
    }

    public void setDateOfBirth(String dateOfBirth)
    {
        this.dateOfBirth = dateOfBirth;
    }

    public String getAge()
    {
        return age;
    }

    public void setAge(String age)
    {
        this.age = age;
    }

    public Date getMaxDate()
    {
        return maxDate;
    }

    public void setMaxDate(Date maxDate)
    {
        this.maxDate = maxDate;
    }

}
